package bidsanity

import grails.plugin.jms.Queue
import grails.converters.JSON
import org.codehaus.groovy.grails.web.mapping.LinkGenerator

class ListingCompleteService {

    LinkGenerator grailsLinkGenerator
    def mailService
    static exposes = ["jms"]

    @Queue(name='listing.complete')
    def sendComplete(msg) {

        def data = JSON.parse(msg)

        def link = grailsLinkGenerator.link(controller: 'listing', action: 'show', id: data.listingId, absolute: true)


        mailService.sendMail {
            to "$data.sellerEmail","$data.winnerEmail"
            from "listings@bidsanity.com"
            subject "Listing complete for: $data.listingDescription"
            body "The listing for $data.listingDescription has completed for a price of \$$data.winningAmount.  " +
                 "Seller please contact the buyer to arrange for payment. View the listing at: $link"
        }

        return null
    }

    @Queue(name='listing.nobids')
    def sendNoBids(msg) {

        def data = JSON.parse(msg)

        def link = grailsLinkGenerator.link(controller: 'listing', action: 'show', id: data.listingId, absolute: true)

        mailService.sendMail {
            to "$data.sellerEmail"
            from "listings@bidsanity.com"
            subject "Listing complete with no bids for: $data.listingDescription"
            body "The listing for $data.listingDescription has completed with no bids. View the listing at $link"
        }
        return null
    }
}
